package com.white.upload;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 文件上传
 */
@Slf4j
public class Upload {


    /**
     * 获取图片资源的根路径
     */
    public static final String ROOT;

    public static final String TARGET_PATH;

    public static final String SUFFIX = "/upload/images";


    static {
        String path = Upload.class.getClassLoader().getResource("//").getPath();
        ROOT = path.substring(0, path.length() - 16) + "/src/main/resources/static" + SUFFIX;
        TARGET_PATH = path + "static" + SUFFIX;
    }

    public Boolean upload(MultipartFile multipartFile) {
        String path = ROOT + "/" + multipartFile.getOriginalFilename();
        String targetPath = TARGET_PATH + "/" + multipartFile.getOriginalFilename();
        try {
//            multipartFile.transferTo(new File(path));
            multipartFile.transferTo(new File(targetPath));
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}
